package com.ws.q1.controller;
import com.ws.q1.bean.LanMu;
import com.ws.q1.service.LanMuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.sql.SQLException;
import java.util.List;


@Controller
@RequestMapping("/lanmu")
public class LanmuController {
    @Autowired
    private LanMuService lanmuService;
    @RequestMapping("/add")
    public String add(LanMu lanMu) {
        int add = lanmuService.add(lanMu.getName(), lanMu.getLan_order());
        if (add > 0) {
            return "redirect:/lanmu/list";
        } else {
            return "redirect:/lanmu/list";
        }
    }
    

    @RequestMapping("/delete")
    public String delete(Integer id) {
        int result = lanmuService.deleteById(id);
        return "redirect:/lanmu/list";
    }



    @RequestMapping("/update")
    public String update(LanMu lanMu) {
        lanmuService.update(lanMu);
        return "redirect:/lanmu/list";
    }

    @RequestMapping("/huixian")
    public String huixian(Integer id, Model model) {
        System.out.println(id);
        LanMu huixian = lanmuService.huixian(id);
        System.out.println(huixian);
        model.addAttribute("huixian", huixian);
        return "lanmu/update";
    }




    @RequestMapping("list")
    public String list(Model model) throws SQLException {
        List<LanMu> list = lanmuService.list();
        for (LanMu user : list){
            System.out.println(user);
        }
        model.addAttribute("list",list);
        return "lanmu/list";
    }


    @RequestMapping("/tiaozhuan")
    public String tiaozhuan() {
        return "lanmu/add";
    }





}
